<?php
namespace app\api\controller;
use app\admin\model\articel\Article as ArticleModel;
use app\common\controller\Api;
use app\admin\model\Company as CompanyModel;
use app\common\service\ShumaiApiService;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Validate;

class Company extends Api{

    protected $noNeedRight = '*';
    protected $noNeedLogin = ['certifieCompnany','brands','certifieCompnanyDetails','brandsDetails'];


    public function _initialize()
    {
        parent::_initialize();
    }

    public function info(){
        $info = CompanyModel::get(["user_id"=>$this->auth->getUser()->id]);
        if(empty($info)){
            $this->error("未查询到公司信息");
        }else{
            $this->success('success', $info);
        }
    }

    public function updateinfo(){
//        $data = $this->request->post();
        $uid= $this->auth->getUser()->id;
        $companyName = $this->request->post('name'); //企业名称
        $creditNo = $this->request->post('code');//企业编码
//        $legalPerson = $this->request->post('legal_person');
        $idCard = $this->request->post('id_card');
        $license = $this->request->post('license');
        $legalPerson = $this->request->post('leader_name');
        $leader_phone = $this->request->post('leader_phone');
//        $leader_email = $this->request->post('leader_email');
        $address = $this->request->post('address');
        if (empty($companyName) || empty($creditNo) || empty($legalPerson) || empty($idCard) || empty($license)  || empty($leader_phone) || empty($address)){
            if (!Validate::regex($leader_phone, "^1\d{10}$")) {
                $this->error("手机号格式不正确");
            }
            $this->error('参数不完整');
        }
        $shumaiApiService = new ShumaiApiService();
        $result = $shumaiApiService->companyThreeElementCheck($companyName, $creditNo, $legalPerson);;

//         if($result['data'] == null){
//             $this->error('认证失败');
//         }
//        if (0 === $result['code']) {
//            $this->error($result['msg']);
//        }
//        $checkData = $result['data'];
//        if($checkData['companyName'] ==true && $checkData['creditNo']==true  && $checkData['legalPerson']==true){
            $company = CompanyModel::get(["user_id"=>$this->auth->getUser()->id]);
            $insertData=[
                'name'=>$companyName,
                'code'=>$creditNo,
                'license'=>$license,
                'id_card'=>$idCard,
                'address'=>$address,
                'leader_name'=>$legalPerson,
                'leader_phone'=>$leader_phone,
//                'leader_email'=>$leader_email,
                'user_id'=>$uid,
                'update_time'=>date('Y-m-d H:i:s',time()),
            ];
            Db::startTrans();
            try {
                if($company){
                    Db::name('company')->where(['id'=>$company['id']])->update($insertData);
                }else{
                    $insertData['create_time']= date('Y-m-d H:i:s',time());
                    Db::name('company')->insert($insertData);
                }
                Db::name('user')->where(['id'=>$uid])->update(['verification'=>1]);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            $this->success('返回成功', $result['data']);
//        }
//
//       $this->error('添加失败');

    }

    /**
     * 认证服务过的企业
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/25 10:12
     *
     */
    public  function  certifieCompnany(){
        $where=[
            'status'=>1,
            'delete_time'=>0
        ];
//        $params =$this->request->param();
        $list = (new \app\admin\model\CertifiedCompany())->where($where)->order(['sort'=>'desc','id'=>'desc'])
            ->field('id,company_name,title,desc,image,content')
            ->paginate($this->request->param('list_rows', 10));;
        $this->success("success", $list);
    }

    public  function  certifieCompnanyDetails(){
        $params=$this->request->param();
        $where=[
            'id'=>$params['id'],
            'status'=>1,
            'delete_time'=>0
        ];
//        $params =$this->request->param();
        $list = (new \app\admin\model\CertifiedCompany())->where($where)->order(['sort'=>'desc','id'=>'desc'])
            ->field('id,company_name,title,desc,image,content')
            ->find();
        $this->success("success", $list);
    }


    /**
     * 合作品牌
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/25 10:12
     */
    public  function  brands(){
        $where=[
            'status'=>1,
            'delete_time'=>0
        ];
        $list = (new \app\admin\model\cooperate\Brand())->where($where)->order(['sort'=>'desc','id'=>'desc'])
            ->field('id,company_name,title,desc,image,content')
            ->paginate($this->request->param('list_rows', 10));;
        $this->success("success", $list);


    }

    public  function  brandsDetails(){  //政府支持
        $params=$this->request->param();
        $where=[
            'id'=>$params['id'],
            'status'=>1,
            'delete_time'=>0
        ];
//        $params =$this->request->param();
        $list = (new \app\admin\model\cooperate\Brand())->where($where)->order(['sort'=>'desc','id'=>'desc'])
            ->field('id,company_name,title,desc,image,content')
            ->find();
        $this->success("success", $list);
    }


}
